package com.timebank.volunteer.service.impl;

import com.timebank.common.core.constant.VolunteerExceptionConstant;
import com.timebank.common.core.exception.volunteer.VolunteerException;
import com.timebank.common.security.utils.SecurityUtils;
import com.timebank.volunteer.mapper.VolunteerBlackListMapper;
import com.timebank.volunteer.mapper.VolunteerMapper;
import com.timebank.volunteer.pojo.entity.Volunteer;
import com.timebank.volunteer.pojo.entity.VolunteerBlackList;
import com.timebank.volunteer.pojo.vo.VolunteerBlackListVO;
import com.timebank.volunteer.service.VolunteerBlackListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 志愿服务黑名单 实现类
 * @author shiliuyinzhen
 */
@Service
public class VolunteerBlacklistServiceImpl implements VolunteerBlackListService{

    @Autowired
    private VolunteerBlackListMapper volunteerBlackListMapper;

    @Autowired
    private VolunteerMapper volunteerMapper;

    /**
     * 根据用户Ids和志愿服务Id新增黑名单
     * @param userIds
     * @param volunteerId
     */
    @Override
    public int insertBlackListByUserIdsAndVolunteerId(Long[] userIds, Long volunteerId) {
        List<VolunteerBlackList> volunteerBlackLists = new ArrayList<>();
        for (Long userId : userIds) {
            //初始化黑名单数据
            VolunteerBlackList volunteerBlackList = VolunteerBlackList.builder()
                    .volunteerId(volunteerId)
                    .userId(userId)
                    .createId(SecurityUtils.getUserId())
                    .createBy(SecurityUtils.getUsername())
                    .createTime(LocalDateTime.now())
                    .updateTime(LocalDateTime.now())
                    .build();
            volunteerBlackLists.add(volunteerBlackList);
        }
        return volunteerBlackListMapper.BatchInsertBlackList(volunteerBlackLists);
    }

    /**
     * 根据志愿服务Id查询黑名单
     * @param volunteerId
     * @return
     */
    @Override
    public List<VolunteerBlackListVO> getBlackListByVolunteerId(Long volunteerId) {
        //校验用户权限
        checkUserPermission(volunteerId);
        VolunteerBlackList volunteerBlackList =  VolunteerBlackList.builder()
                .volunteerId(volunteerId)
                .createId(SecurityUtils.getUserId())
                .build();
        return volunteerBlackListMapper.getBlackList(volunteerBlackList);
    }

    /**
     * 校验用户权限
     * @param volunteerId
     */
    private void checkUserPermission(Long volunteerId) {
        //查询志愿服务
        Volunteer volunteer = volunteerMapper.getVolunteerById(volunteerId);
        if (volunteer == null){
            throw new VolunteerException(VolunteerExceptionConstant.VOLUNTEER_NOT_FOUND +",id: " + volunteerId);
        }
        Long userId = SecurityUtils.getUserId();
        //如果该用户不是管理员，也不是志愿服务的发布人
        if (!SecurityUtils.isAdmin(userId) && volunteer.getPublisherId() != userId){
            //没有相关权限
            throw new VolunteerException(VolunteerExceptionConstant.USER_NOT_HAVE_PERMISSION);
        }
    }

    /**
     * 根据志愿服务Id和用户Ids批量移除黑名单
     * @param volunteerId
     * @param userIds
     * @return
     */
    @Override
    public int removeBlackListByVolunteerIdAndUserIds(Long volunteerId, Long[] userIds) {
        //校验用户权限
        checkUserPermission(volunteerId);
        List<VolunteerBlackList> volunteerBlackLists = new ArrayList<>();
        for (Long id : userIds) {
            VolunteerBlackList volunteerBlackList = VolunteerBlackList.builder()
                    .volunteerId(volunteerId)
                    .userId(id)
                    .createId(SecurityUtils.getUserId())
                    .updateTime(LocalDateTime.now())
                    .build();
            volunteerBlackLists.add(volunteerBlackList);
        }
        return volunteerBlackListMapper.BatchRemoveBlackList(volunteerBlackLists);
    }

    /**
     * 根据志愿服务Id和用户Id查询黑名单
     * @param volunteerId
     * @param userId
     * @return
     */
    @Override
    public List<VolunteerBlackListVO> getBlackListByVolunteerIdAndUserId(Long volunteerId, Long userId) {
        VolunteerBlackList volunteerBlackList = VolunteerBlackList.builder()
                .volunteerId(volunteerId)
                .userId(userId)
                .build();
        return volunteerBlackListMapper.getBlackList(volunteerBlackList);
    }
}
